<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/6/8
 * Time: 10:16
 */

namespace app\common\model;

use think\Model;

class UserCommission extends Model
{
    protected $name = "user_commission";

    /**
     * @param $uid // 商家用户id
     * @param $amount
     */
    public function addUserCommission($uid, $amount, $type, $orderId)
    {
        if ($amount == 0){
             return false;
        }
        $original_price =  $amount;
        $userModel = new User();
        $amountModel = new  AmountLog();
        $userInfo = $userModel->where(['id' => $uid])->find();
        if ($type == 1){
            $fees = Config::getConfigValue('dazi_fees');
            $typeName ='dazi_income';
            $operationSource  = 9;
            $userOperationSource  = 4;
        }elseif ($type == 2){
            $fees = Config::getConfigValue('service_fees');
            $typeName ='service_income';
            $operationSource  = 10;
            $userOperationSource  = 8;
        }elseif ($type == 3){
            $fees = 1;
            $operationSource  = 14;
            $userOperationSource  = 13;
        }
        $platformAmount = bcdiv(bcmul((string)$amount, (string)$fees, 2), '100', 2);
        $amount = bcsub((string)$amount, (string)$platformAmount, 2);
        $firstCommission = Config::getConfigValue('first_commission');
        if ($firstCommission > 0 && $userInfo['pid'] > 0) {
            $firstCommissionData = [
                'trigger_uid' => $uid,
                'receive_uid' => $userInfo['pid'],
                'level' => 1,
                'amount' => bcdiv(bcmul($amount, $firstCommission, 2), 100, 2),
                'ratio' => $firstCommission,
                'create_time' => time(),
                'type' => $type,
                'order_id' => $orderId,
            ];
            if ($firstCommissionData['amount']> 0){
                $this->insert($firstCommissionData);
                $amount  = bcsub($amount,$firstCommissionData['amount'],2);
                $userModel->where(['id' => $userInfo['pid']])
                    ->inc('first_commission_income', $firstCommissionData['amount'])
                    ->inc('total_income', $firstCommissionData['amount'])
                    ->inc('surplus_income',$firstCommissionData['amount'])
                    ->update();
                $firstAmountLog = [
                    'uid' => $userInfo['pid'],
                    'amount' => $firstCommissionData['amount'],
                    'amount_type' => 2,
                    'operation_source' => $operationSource,
                    'amount_direction' => 1,
                    'create_time' => time(),
                    'correlation_id' => $orderId,
                    'ratio' => $firstCommission,
                    'handling_fees' => 0,
                ];
                $amountModel->insert($firstAmountLog);
            }
            $secondCommission = Config::getConfigValue('second_commission');
            $pUserInfo = $userModel->where(['id' => $userInfo['pid']])->find();/**/
            if ($secondCommission > 0 && $pUserInfo['pid'] > 0) {
                $secondCommissionData = [
                    'trigger_uid' => $uid,
                    'receive_uid' => $pUserInfo['pid'],
                    'level' => 2,
                    'amount' => bcdiv(bcmul($amount, $secondCommission, 2), 100, 2),
                    'ratio' => $secondCommission,
                    'create_time' => time(),
                    'type' => $type,
                    'order_id' => $orderId,
                ];
                if ($secondCommissionData['amount']> 0){

                $this->insert($secondCommissionData);
                    $amount  = bcsub($amount,$secondCommissionData['amount'],2);
                    $userModel->where(['id' => $pUserInfo])
                        ->inc('second_commission_income', $secondCommissionData['amount'])
                        ->inc('total_income', $secondCommissionData['amount'])
                        ->inc('surplus_income', $secondCommissionData['amount'])
                        ->update();
                    $amountLog = [
                        'uid' => $uid,
                        'amount' => $amount,
                        'amount_type' => 2,
                        'operation_source' => $operationSource,
                        'amount_direction' => 1,
                        'create_time' => time(),
                        'correlation_id' => $orderId,
                        'ratio' => $secondCommission,
                        'handling_fees' => 0,
                    ];
                    $amountModel->insert($amountLog);
                }


            }
        }
        $recharge_ratio  = Config::getConfigValue('recharge_ratio');
        if ($type == 1 || $type == 2 ){
            $userModel->where(['id' => $uid])
                ->inc('total_income', (float)$amount)
                ->inc('surplus_income', (float)$amount)
                ->inc($typeName, (float)$amount)
                ->update();
            $amountLog = [
                'uid' => $uid,
                'amount' => $amount,
                'amount_type' => 2,
                'operation_source' => $userOperationSource,
                'amount_direction' => 1,
                'create_time' => time(),
                'correlation_id' => $orderId,
                'ratio' => $fees,
                'handling_fees_amount' => bcdiv($platformAmount,$recharge_ratio,2),
                'handling_fees' => $platformAmount,
                'original_price' =>$original_price
            ];
            $amountModel->insert($amountLog);
        }else{
           /* $userModel->where(['id' => $uid])
                ->inc('total_income', (float)$amount)
                ->inc('surplus_income', (float)$amount)
                ->update();*/
            $amountModel = new AmountLog();
            $amountData  = [
                'uid' =>$uid,
                'amount' => $original_price,
                'amount_type' => 1,
                'amount_direction' => 2,
                'create_time' => time(),
                'correlation_id' => $orderId,
                'operation_source' => 12,
                'handling_fees_amount' => bcdiv($amount,$recharge_ratio,2),
                'handling_fees' => $amount,
                'original_price' =>$original_price
            ];
            $amountModel->insert($amountData);
        }
        return true;

    }

    public function newUserReward($uid,$pid){
        if ($pid > 0){
            $new_user_reward = Config::getConfigValue('new_user_reward');
            if( $new_user_reward> 0){
                $firstCommissionData = [
                    'trigger_uid' => $uid,
                    'receive_uid' => $pid,
                    'level' => 1,
                    'amount' => $new_user_reward,
                    'ratio' => 1,
                    'create_time' => time(),
                    'type' => 4,
                    'order_id' => 0,
                ];
                $this->insert($firstCommissionData);
                $userModel = new User();
                $userModel->where(['id' => $pid])
                    ->inc('first_commission_income', $new_user_reward)
                    ->inc('total_income',$new_user_reward)
                    ->inc('surplus_income',$new_user_reward)
                    ->update();
                $firstAmountLog = [
                    'uid' => $pid,
                    'amount' => $firstCommissionData['amount'],
                    'amount_type' => 2,
                    'operation_source' => 22,
                    'amount_direction' => 1,
                    'create_time' => time(),
                    'correlation_id' => 0,
                    'ratio' => 0,
                    'handling_fees' => 0,
                ];
                $amountModel = new AmountLog();
                $amountModel->insert($firstAmountLog);
            }
        }

        return true;
    }

    public function vipCommission($uid,$id,$first_commission,$second_commission){

        if ($first_commission >0){
            $userModel = new User();
            $userInfo = $userModel->where(['id' => $uid])->find();
            if ($userInfo['pid'] > 0){
                $firstCommissionData = [
                    'trigger_uid' => $uid,
                    'receive_uid' => $userInfo['pid'],
                    'level' => 1,
                    'amount' => $first_commission,
                    'ratio' => 1,
                    'create_time' => time(),
                    'type' => 3,
                    'order_id' => $id,
                ];
                $this->insert($firstCommissionData);
                $userModel = new User();
                $userModel->where(['id' => $userInfo['pid']])
                    ->inc('first_commission_income', $first_commission)
                    ->inc('vip_income', $first_commission)
                    ->inc('total_income',$first_commission)
                    ->inc('surplus_income',$first_commission)
                    ->update();
                $firstAmountLog = [
                    'uid' =>  $userInfo['pid'],
                    'amount' => $firstCommissionData['amount'],
                    'amount_type' => 2,
                    'operation_source' => 24,
                    'amount_direction' => 1,
                    'create_time' => time(),
                    'correlation_id' => 0,
                    'ratio' => 0,
                    'handling_fees' => 0,
                ];
                $amountModel = new AmountLog();
                $amountModel->insert($firstAmountLog);

                if ($second_commission > 0){
                    $puserInfo = $userModel->where(['id' => $userInfo['pid']])->find();
                    if ($puserInfo['pid'] > 0){
                        $secondCommissionData = [
                            'trigger_uid' => $uid,
                            'receive_uid' => $puserInfo['pid'],
                            'level' => 2,
                            'amount' => $second_commission,
                            'ratio' => 1,
                            'create_time' => time(),
                            'type' => 4,
                            'order_id' => $id,
                        ];
                        $this->insert($secondCommissionData);
                        $userModel = new User();
                        $userModel->where(['id' => $puserInfo['pid']])
                            ->inc('vip_income', $first_commission)
                            ->inc('second_commission_income', $second_commission)
                            ->inc('total_income',$second_commission)
                            ->inc('surplus_income',$second_commission)
                            ->update();
                        $secondAmountLog = [
                            'uid' =>  $puserInfo['pid'],
                            'amount' => $secondCommissionData['amount'],
                            'amount_type' => 2,
                            'operation_source' => 24,
                            'amount_direction' => 1,
                            'create_time' => time(),
                            'correlation_id' => 0,
                            'ratio' => 0,
                            'handling_fees' => 0,
                        ];
                        $amountModel = new AmountLog();
                        $amountModel->insert($secondAmountLog);
                    }

                }

            }

        }

        return true;
    }

    public function rechargeCommission($uid,$id,$amount){
        $first_commission =  bcdiv(bcmul(Config::getConfigValue('first_commission'), $amount,2),100,2);
        $second_commission = bcdiv(bcmul(Config::getConfigValue('second_commission'),$amount,2),100,2);
        if ($first_commission >0){
            $userModel = new User();
            $userInfo = $userModel->where(['id' => $uid])->find();
            if ($userInfo['pid'] > 0){
                $firstCommissionData = [
                    'trigger_uid' => $uid,
                    'receive_uid' => $userInfo['pid'],
                    'level' => 1,
                    'amount' => $first_commission,
                    'ratio' => 1,
                    'create_time' => time(),
                    'type' => 5,
                    'order_id' => $id,
                ];
                $this->insert($firstCommissionData);
                $userModel = new User();
                $userModel->where(['id' => $userInfo['pid']])
                    ->inc('first_commission_income', $first_commission)
                   // ->inc('vip_income', $first_commission)
                    ->inc('total_income',$first_commission)
                    ->inc('surplus_income',$first_commission)
                    ->update();
                $firstAmountLog = [
                    'uid' =>  $userInfo['pid'],
                    'amount' => $firstCommissionData['amount'],
                    'amount_type' => 2,
                    'operation_source' => 23,
                    'amount_direction' => 1,
                    'create_time' => time(),
                    'correlation_id' => $id,
                    'ratio' => 0,
                    'handling_fees' => 0,
                ];
                $amountModel = new AmountLog();
                $amountModel->insert($firstAmountLog);

                if ($second_commission > 0){
                    $puserInfo = $userModel->where(['id' => $userInfo['pid']])->find();
                    if ($puserInfo['pid']>0){
                        $secondCommissionData = [
                            'trigger_uid' => $uid,
                            'receive_uid' => $puserInfo['pid'],
                            'level' => 2,
                            'amount' => $second_commission,
                            'ratio' => 1,
                            'create_time' => time(),
                            'type' => 5,
                            'order_id' => $id,
                        ];
                        $this->insert($secondCommissionData);
                        $userModel = new User();
                        $userModel->where(['id' => $puserInfo['pid']])
                            //->inc('vip_income', $first_commission)
                            ->inc('second_commission_income', $second_commission)
                            ->inc('total_income',$second_commission)
                            ->inc('surplus_income',$second_commission)
                            ->update();
                        $secondAmountLog = [
                            'uid' =>  $puserInfo['pid'],
                            'amount' => $secondCommissionData['amount'],
                            'amount_type' => 2,
                            'operation_source' => 23,
                            'amount_direction' => 1,
                            'create_time' => time(),
                            'correlation_id' => $id,
                            'ratio' => 0,
                            'handling_fees' => 0,
                        ];
                        $amountModel = new AmountLog();
                        $amountModel->insert($secondAmountLog);
                    }

                }

            }

        }

        return true;
    }


}
